#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <strings.h>
#include <ctype.h>
#define MAXLINE 80
#define SERV_PORT 6666
int main(int argc, char *argv[])
{
    struct sockaddr_in servaddr;
    int sockfd, n;
    char buf[MAXLINE];
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
    servaddr.sin_port = htons(SERV_PORT);
     //发送数据到客户端
    while (fgets(buf, MAXLINE, stdin) != NULL) {
        n =sendto(sockfd, buf, strlen(buf),0,(struct sockaddr *)&servaddr,
                  sizeof(servaddr));
        if (n == -1)
            perror("sendto error");
          //接收客户端返回的数据
         n = recvfrom(sockfd, buf, MAXLINE, 0, NULL, 0);
         if (n == -1)
             perror("recvfrom error");
          //将接收到的数据打印到终端
         send(STDOUT_FILENO, buf, n,0);
     }
     close(sockfd);
     return 0;
}
